//
//  CBCStream.m
//  Cbchot
//
//  Created by 李雷川 on 2017/12/23.
//  Copyright © 2017年 王越. All rights reserved.
//

#import "CBCStream.h"
#import "CommonFuctions.h"
@implementation CBCStream
- (instancetype)init
{
    if (self = [super init]) {
      
    }
    return self;
}

+ (instancetype)sharedCBCStream
{
    static CBCStream *cbcStream = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cbcStream = [[CBCStream alloc] init];
    });
    return cbcStream;
}

-(NSString *)readCBCZfFileContent:(NSString *)filePath{
    //内容长度
    self.inputStream = [[NSInputStream alloc]initWithFileAtPath:filePath];
    [self.inputStream open];

    if (![self.inputStream hasBytesAvailable]) {
//        NSLog(@"%@",[NSString stringWithFormat:@"文件流读取失败 地址%@",filePath]);
        return @"";
    }
    
    //头部信息
    Byte headerBuffer[6];
    NSInteger bufferLength =  [self.inputStream read:headerBuffer maxLength:6];
    NSData *data = [NSData dataWithBytes:headerBuffer length:bufferLength];
    NSString *header = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    if([header isEqualToString:@"cbchot"] == NO){
        return  nil;
    }
//    NSLog(@"header is:%@",header);
    
    
    //版本信息
    Byte versionBuffer[4];
    bufferLength =  [self.inputStream read:versionBuffer maxLength:4];
    data = [NSData dataWithBytes:versionBuffer length:bufferLength];
    int version;
    [data getBytes:&version length:sizeof(version)];
//    NSLog(@"version is:%d",version);
    
    
    ///描述信息长度
    Byte desLengthBuffer[8];
    bufferLength =  [self.inputStream read:desLengthBuffer maxLength:8];
    data = [NSData dataWithBytes:desLengthBuffer length:bufferLength];
    int desLength;
    [data getBytes:&desLength length:sizeof(desLength)];
//    NSLog(@"desLength is:%d",desLength);
    
    
    //描述信息内容
    Byte desBuffer[desLength];
    bufferLength =  [self.inputStream read:desBuffer maxLength:desLength];
    data = [NSData dataWithBytes:desBuffer length:bufferLength];
//    NSString *des = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//    NSLog(@"des is:%@",des);
    
    
    //文本开始位置
    Byte startPostionBuffer[8];
    bufferLength =  [self.inputStream read:startPostionBuffer maxLength:8];
    int startPostion;
    [data getBytes:&startPostion length:sizeof(startPostion)];
//    NSLog(@"startPostion is:%d",startPostion);
    
    
    //内容长度
    Byte contentLenghtBuffer[8];
    bufferLength =  [self.inputStream read:contentLenghtBuffer maxLength:8];
    int contentLength;
    [data getBytes:&contentLength length:sizeof(contentLength)];
//    NSLog(@"contentLength is:%d",contentLength);
    
    Byte content[1024];
    bufferLength =  [self.inputStream read:content maxLength:1024];
    NSMutableData *mutableData = [NSMutableData dataWithBytes:content length:bufferLength];
    while (bufferLength > 0) {
        Byte content[1024];
        bufferLength =  [self.inputStream read:content maxLength:1024];
        NSData *newData = [NSData dataWithBytes:content length:bufferLength];
        [mutableData appendData:newData];
    }
    
    NSData *uncompressDataGZip = [CommonFuctions gzipUncompress:mutableData];
    NSString* stringRes = [[NSString alloc] initWithData:uncompressDataGZip encoding:NSUTF16LittleEndianStringEncoding];
    NSString * str = [stringRes stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return str;
}


@end
